import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import javax.swing.ButtonGroup;

public class CakeOrderingSystem extends JFrame {

	private JPanel contentPane;
	private JLabel lblTypeCake;
	private JLabel lblFlavors;
	private JComboBox comboCakeType;
	private JComboBox comboFlavors;
	private JLabel lblSizePrice;
	private JRadioButton rdoRegular;
	private JRadioButton rdoLarge;
	private JLabel lblAddCandles;
	private JButton btnCandleMinus;
	private JLabel lblCandle1;
	private JButton btnCandlePlus;
	private JLabel lblAddLayer;
	private JButton btnLayerMinus;
	private JLabel lblLayer1;
	private JButton btnLayerPlus;
	private JButton btnBillOut;
	private JLabel lblOrderDetails;
	private JTextArea textAreaOrderDetails;
	private JLabel lblRegularPrice;
	private JLabel lblLargePrice;
	private final ButtonGroup buttonGroup = new ButtonGroup();
    private int addCandles = 1, addlayers = 1;
    private double candlePrice = 10.00, layerPrice = 50.00;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CakeOrderingSystem frame = new CakeOrderingSystem();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	
	////////METHODS//////////////
	
	private String getCakeSize() {
		String size = "";
		if(rdoRegular.isSelected()) {
			size = rdoRegular.getText();
		}
		else
			size = rdoLarge.getText();
		
		return size;
	}
	
	private double getCakePrice() {
		double price = 0;
		
		if(rdoRegular.isSelected()) {
			price = Double.parseDouble(lblRegularPrice.getText());
		}
		else
			price = Double.parseDouble(lblLargePrice.getText());
		return price;
	}
	
	private double compute() {
		double totalPrice = 0;
		totalPrice = totalPrice + getCakePrice() + candlePrice + layerPrice; 
		return totalPrice;
	}
	
	private void orderDetails() {
		textAreaOrderDetails.setText("CAKE: \t" + comboCakeType.getSelectedItem() + "\n\nFLAVOR: \t" + comboFlavors.getSelectedItem()
		                             + "\n\nSIZE: \t" + getCakeSize() + "\n\nPRICE: \t" + getCakePrice()
		                             + "\n\nCANDLES: \t" + lblAddCandles.getText() + "x" + lblCandle1.getText() + "\t\t" + candlePrice 
		                             + "\n\nLAYERS: \t" + lblAddLayer.getText() + "x" + lblLayer1.getText() 
		                             + "\t" + layerPrice + "\n\n*****************************************"
		                             + "\n\nTOTAL PRICE: \t" + compute());
		
		Object cakeType, cakeFlovor, price, candlesNo, layersNo, orderD;
		
		cakeType = comboCakeType.getSelectedItem();
		cakeFlovor = comboFlavors.getSelectedItem();
		price = getCakePrice();
		candlesNo = lblCandle1.getText();
		layersNo = lblLayer1.getText();
		orderD = textAreaOrderDetails.getText();
		
		try {
			
			//Class.forName(com.mysql.jdbc.Driver);
			
			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cake_ordering", "root", "");
		    PreparedStatement ps = conn.prepareStatement("INSERT INTO order_details(cake_type,cake_flavor,price,candles,layers) VALUES (?,?,?,?,?)");
			ps.setString(1, (String) comboCakeType.getSelectedItem());
			ps.setString(2, (String) comboFlavors.getSelectedItem());
			ps.setDouble(3, getCakePrice());
			ps.setString(4, lblCandle1.getText());
			ps.setString(5, lblLayer1.getText());
			int x = ps.executeUpdate();
			if(x > 0) {
				JOptionPane.showMessageDialog(null, "Order Details Saved.", null, JOptionPane.YES_OPTION);
				}
			else {
				JOptionPane.showMessageDialog(null, "Order Details Not Saved.", null, JOptionPane.ERROR_MESSAGE);
			}
		}
		catch(Exception e1) {
			System.out.println(e1);
		}

	}

	
	
	public CakeOrderingSystem() {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowOpened(WindowEvent e) {
				comboCakeType.addItem("Birthday Cake");
				comboCakeType.addItem("Wedding Cake");
			}
		});
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 553, 488);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBackground(Color.LIGHT_GRAY);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		lblTypeCake = new JLabel("Types of Cake:");
		lblTypeCake.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblTypeCake.setBounds(10, 21, 169, 20);
		contentPane.add(lblTypeCake);
		
		comboCakeType = new JComboBox();
		comboCakeType.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//comboCakeType.removeAllItems();
				if(comboCakeType.getSelectedIndex() == 0) {
					comboFlavors.addItem("Chocolate");
					comboFlavors.addItem("Mocha");
					lblRegularPrice.setText("500.00");
					lblLargePrice.setText("850.00");
				}
				else if(comboCakeType.getSelectedIndex() == 1){
					comboFlavors.addItem("Chocolate");
					comboFlavors.addItem("Mocha");
					lblRegularPrice.setText("1200.00");
					lblLargePrice.setText("1500.00");
				}
			}
		});
		comboCakeType.setBounds(20, 52, 193, 22);
		contentPane.add(comboCakeType);
		
		comboFlavors = new JComboBox();
		comboFlavors.setBounds(20, 117, 193, 22);
		contentPane.add(comboFlavors);
		
		lblFlavors = new JLabel("Flavors:");
		lblFlavors.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblFlavors.setBounds(10, 86, 169, 20);
		contentPane.add(lblFlavors);
		
		lblSizePrice = new JLabel("Size & Price:");
		lblSizePrice.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblSizePrice.setBounds(10, 153, 169, 20);
		contentPane.add(lblSizePrice);
		
		rdoRegular = new JRadioButton("Regular");
		buttonGroup.add(rdoRegular);
		rdoRegular.setBackground(Color.LIGHT_GRAY);
		rdoRegular.setBounds(20, 180, 77, 23);
		contentPane.add(rdoRegular);
		
		rdoLarge = new JRadioButton("Large");
		buttonGroup.add(rdoLarge);
		rdoLarge.setBackground(Color.LIGHT_GRAY);
		rdoLarge.setBounds(20, 206, 77, 23);
		contentPane.add(rdoLarge);
		
		lblAddCandles = new JLabel("Add Candles:");
		lblAddCandles.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblAddCandles.setBounds(10, 236, 169, 20);
		contentPane.add(lblAddCandles);
		
		btnCandleMinus = new JButton("-");
		btnCandleMinus.setBackground(Color.YELLOW);
		btnCandleMinus.setFont(new Font("Tahoma", Font.BOLD, 20));
		btnCandleMinus.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addCandles--;
				candlePrice -= 10;
				lblCandle1.setText(String.valueOf(addCandles));
			}
		});
		btnCandleMinus.setBounds(10, 267, 54, 48);
		contentPane.add(btnCandleMinus);
		
		lblCandle1 = new JLabel("1");
		lblCandle1.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblCandle1.setHorizontalAlignment(SwingConstants.CENTER);
		lblCandle1.setBounds(88, 270, 49, 48);
		contentPane.add(lblCandle1);
		
		btnCandlePlus = new JButton("+");
		btnCandlePlus.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addCandles++;
				candlePrice += 10;
				lblCandle1.setText(String.valueOf(addCandles));
			}
		});
		btnCandlePlus.setBackground(Color.GREEN);
		btnCandlePlus.setFont(new Font("Tahoma", Font.BOLD, 16));
		btnCandlePlus.setBounds(159, 267, 54, 48);
		contentPane.add(btnCandlePlus);
		
		lblAddLayer = new JLabel("Add Layer:");
		lblAddLayer.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblAddLayer.setBounds(10, 327, 169, 20);
		contentPane.add(lblAddLayer);
		
		btnLayerMinus = new JButton("-");
		btnLayerMinus.setBackground(Color.YELLOW);
		btnLayerMinus.setFont(new Font("Tahoma", Font.BOLD, 20));
		btnLayerMinus.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addlayers--;
				layerPrice -= 50.00;
				lblLayer1.setText(String.valueOf(addlayers));
			}
		});
		btnLayerMinus.setBounds(10, 358, 54, 48);
		contentPane.add(btnLayerMinus);
		
		lblLayer1 = new JLabel("1");
		lblLayer1.setHorizontalAlignment(SwingConstants.CENTER);
		lblLayer1.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblLayer1.setBounds(88, 360, 49, 48);
		contentPane.add(lblLayer1);
		
		btnLayerPlus = new JButton("+");
		btnLayerPlus.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addlayers++;
				layerPrice += 50.00;
				lblLayer1.setText(String.valueOf(addlayers));
			}
		});
		btnLayerPlus.setBackground(Color.GREEN);
		btnLayerPlus.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnLayerPlus.setBounds(159, 358, 54, 48);
		contentPane.add(btnLayerPlus);
		
		btnBillOut = new JButton("Bill Out");
		btnBillOut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				orderDetails();
			}
		});
		btnBillOut.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnBillOut.setBounds(42, 417, 111, 23);
		contentPane.add(btnBillOut);
		
		lblOrderDetails = new JLabel("Order Details:");
		lblOrderDetails.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblOrderDetails.setBounds(241, 24, 169, 20);
		contentPane.add(lblOrderDetails);
		
		textAreaOrderDetails = new JTextArea();
		textAreaOrderDetails.setBounds(241, 51, 280, 389);
		contentPane.add(textAreaOrderDetails);
		
		lblRegularPrice = new JLabel("");
		lblRegularPrice.setBounds(140, 184, 73, 14);
		contentPane.add(lblRegularPrice);
		
		lblLargePrice = new JLabel("");
		lblLargePrice.setBounds(140, 210, 73, 14);
		contentPane.add(lblLargePrice);
	}
}
